# import data_read
'''
data_2 = data_read.data_2
print("success")
'''
import numpy as np
import pandas as pd


interval_3_0 = [[600, 650],
                [650, 690],
                [690, 740],
                [740, 780],
                [780, 840],
                [840, 887],
                [887, 945],
                [945, 1085],
                [1085, 1156],
                [1156, 1267],
                [1267, 1392],
                [1392, 1432],
                [1432, 1498],
                [1498, 1566],
                [1566, 1655],
                [1655, 1820],
                [2828, 2875],
                [2875, 2995],
                [2995, 3666],
                ]
interval_3_1 = [[4190, 4370],
                [4370, 4490],
                [4490, 4650],
                [4650, 4982],
                [4982, 5370],
                [5955, 6110],
                [6110, 6490],
                [6490, 7210],
                ]

# 读取数据

data_3_0 = pd.read_excel(io='adjuncts/adjunct_3.xlsx', sheet_name="中红外", index_col=0)  # 读取附件三内容
data_3_1 = pd.read_excel(io='adjuncts/adjunct_3.xlsx', sheet_name="近红外", index_col=0)
print("data_3 read success!")

print(data_3_0)
print(data_3_1)

# 数据清洗  去除问题中检材编号  去除产地信息


def peak_interval(left, right, list_row, start):  # 计算区间峰值位置 参数按照检材编号,左闭右开

    lst = list_row[left - start: right - start]          # 切割传入函数数据   list_row是一个list
    # print("===== * =====")
    lst_new = np.ndarray.tolist(lst)             # 数据类型转换
    peak = max(lst_new)                           # 计算峰值
    if peak > 0:
        return [lst_new.index(peak) + left, peak]    # 返回峰值x, y轴坐标
    else:
        return [0, 0]                                # 出现较大干扰值  返回状态码


# =========预处理数据=======

def data_pretreatment(data, interval, start):

    table = []                                            # 二维列表 用来存放预处理数据
    for r in data.index:
        row = data.loc[r].values[0:]             # 按行读取 附件 数据
        if pd.isnull(row[0]):
            #  print("++++++++++++++", r)
            continue
        result_row = [row[0]]                                        # 存放该行数据处理结果
        for i in interval:                                     # 设i变量为区间库滚动游标
            t = peak_interval(i[0], i[1], row, start)                 # t接收返回的峰值 横纵坐标
            result_row.append(t[0])
            result_row.append(t[1])
        table.append(result_row)                          # 将该行数据存入表中
    return table


# print(data_2)

tables_0 = data_pretreatment(data_3_0, interval_3_0, 552-1)   # 减去op列占的列数
tables_1 = data_pretreatment(data_3_1, interval_3_1, 4004-1)
print(tables_0)   # 检验
print(tables_1)



